package com.zsr.example.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.entity.*;
import com.zsr.example.service.*;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.JwtUtil;
import com.zsr.example.util.constants.BusinessEnum;
import com.zsr.example.util.constants.ErrorEnum;
import com.zsr.example.vo.front.PathFrontReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhousiren
 * @date 2022/2/14 16:41
 */
@RestController
@RequestMapping("/eduservice/path")
@CrossOrigin
public class PathController {
    @Autowired
    private EduPathService pathService;
    @Autowired
    private EduPathStepService stepService;
    @Autowired
    private EduPathCategoryService categoryService;
    @Autowired
    private EduPathStepRelationService relationService;
    @Autowired
    private TOrderService orderService;

    @PostMapping("getFrontPathList/{page}/{limit}")
    public JSONObject getFrontPathList(@PathVariable long page, @PathVariable long limit,
                                       @RequestBody(required = false) PathFrontReq pathFrontReq) {
        Page<EduPath> pagePath = new Page<>(page, limit);
        Map<String, Object> map = pathService.getFrontPathList(pagePath, pathFrontReq);
        // 返回分页所有数据
        return CommonUtil.successJson(map);
    }

    /**
     * 获取路径节点
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public JSONObject getPath(HttpServletRequest request, @PathVariable String id) {
        EduPath path = pathService.getById(id);
        if(ObjectUtils.isEmpty(path))throw new CommonJsonException(ErrorEnum.E_90004);
        Map<String, Object> map = new HashMap<>();
        //查询步骤

        List<EduPathStep> stepList = stepService.list(new LambdaQueryWrapper<EduPathStep>()
        .eq(EduPathStep::getPathId, path.getId())
        .orderByAsc(EduPathStep::getSort));

        boolean isBuy = orderService.isBuyCourse(JwtUtil.getMemberIdByJwtToken(request), BusinessEnum.PATH.getType(), id);

        map.put("path", path);
        map.put("step", stepList);
        map.put("isBuy", isBuy);
        //查询关联课程
        if (!CollectionUtils.isEmpty(stepList)) {
            List<String> ids = stepList.stream().map(EduPathStep::getId).collect(Collectors.toList());
            QueryWrapper<EduPathStepRelation> relationQueryWrapper = new QueryWrapper<>();
            relationQueryWrapper.in("step_id", ids);
            Collection<EduPathStepRelation> relations = relationService.list(relationQueryWrapper);
            map.put("relation", relations);
        }

        return CommonUtil.successJson(map);
    }

    @GetMapping("/getCategoryList")
    public JSONObject getCategoryList() {
        List<EduPathCategory> list = categoryService.getListOrderBy();
        return CommonUtil.successJson(list);
    }
}
